﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Data.OracleClient;

public partial class Admin_SanPham : System.Web.UI.Page
{
    DataTable dtsanpham;
    DataTable dt;
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
            HienThiDanhSach();
      

    }

    void HienThiDanhSach()
    {
        // get all sach
        dt = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getAllsach");

        GridView1.DataSource = dt;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
        btninsert.Enabled = false;
    }

    protected void btntmkiem_Click(object sender, EventArgs e)
    {
        GridView1.DataSource= null;
        
        string tensach = txttimsanpham.Text;
        OracleParameter[] thamso = new OracleParameter[]{
            new OracleParameter("p_content",tensach)
        };

        // Search Sach By TenSach
        dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getSachByTenSach", thamso);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        txttimsanpham.Text = "";
    }

    protected void btntaomoi_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        btninsert.Enabled = true;
        btninsert.Visible = true;
        btnupdate.Enabled = false;
        btnupdate.Visible = false;
        txtngaydang.Enabled = false;

        //them vao              
        FillLoaiSach();
        FillNXB();
        //

        txtdongia.Text = txtngaydang.Text = txtnoidung.Text = txtsoluong.Text = txttacgia.Text = txttensach.Text= txttimsanpham.Text = "";
        txtmasach.Text = "";
        txtngaydang.Text = System.DateTime.Now.ToShortDateString();

    }

    void FillNXB()
    {
        DropDownListmaNXB.Items.Clear();
       

        // get all nhaxb
        DataTable dtnxb = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getAllNhaXB");
        if (dtnxb.Rows.Count > 0)
        {
            for (int i = 0; i < dtnxb.Rows.Count; i++)
            {
                ListItem li = new ListItem(dtnxb.Rows[i][1].ToString(), dtnxb.Rows[i][0].ToString());
                DropDownListmaNXB.Items.Add(li);


            }

        }
    }
    //sua lai
    void FillLoaiSach()
    {

        DropDownListmaloai.Items.Clear();
        
        // get all loaisach
        DataTable dtls = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getAllLoaiSach");
        if (dtls.Rows.Count > 0)
        {
            for (int i = 0; i < dtls.Rows.Count; i++)
            {
                ListItem li = new ListItem(dtls.Rows[i][1].ToString(), dtls.Rows[i][0].ToString());
                DropDownListmaloai.Items.Add(li);
                if (dtls.Rows[i][0].ToString() == dtls.Rows[0][0].ToString())
                {
                    DropDownListmaloai.Items[i].Selected = true;
                }

            }

        }
    }



    protected void btnupdate_Click(object sender, EventArgs e)
    {
        txtngaydang.Enabled = true;        

        OracleParameter[] thamso = new OracleParameter[]{
            new OracleParameter("p_masach",txtmasach.Text)
        };
        

        // get Sach By MaSach
        dtsanpham = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getSachByMaSach", thamso);
        string hinh;
        if (FileUpload1.HasFile)
        {
            string strfile = "~/image/" + FileUpload1.FileName;
            string s = Server.MapPath(strfile);
            FileUpload1.SaveAs(s);
            hinh = FileUpload1.FileName;
        }
        else
            hinh = dtsanpham.Rows[0]["Hinh"].ToString(); ;// dtsanpham.Rows[0]["Hinh"].ToString();
        
           
        string masach = txtmasach.Text;
        int manxb = int.Parse(DropDownListmaNXB.SelectedValue);
        int maloaisach = int.Parse(DropDownListmaloai.SelectedValue);
        string tensach = txttensach.Text;
        string tacgia = txttacgia.Text;
        int dongia = int.Parse(txtdongia.Text);
        int soluong = int.Parse(txtsoluong.Text);
        string ngaydang = txtngaydang.Text.Trim();
        //string ngaydang = txtngaydang.Text.Trim();
        string noidung = txtnoidung.Text;
        int namxb = int.Parse(txtnamxb0.Text.Trim());        
        OracleParameter[] thamso1 = new OracleParameter[]
        {
            new OracleParameter("p_masach",masach),
            new OracleParameter("p_tensach",tensach),
            new OracleParameter("p_manhaxb",manxb),
            new OracleParameter("p_maloaisach",maloaisach),
            new OracleParameter("p_tacgia",tacgia),
            new OracleParameter("p_dongia",dongia),
            new OracleParameter("p_soluong",soluong),
            new OracleParameter("p_hinh",hinh),
            //new OracleParameter(":ngaydang",ngaydang),
            new OracleParameter("p_noidung",noidung),
            new OracleParameter("p_namxb",namxb)
        };
     
        // update Sach By MaSach
        DataConnection.GetDatatableByStoreProcedureName("update_SachByMaSach", thamso1);
        this.HienThiDanhSach();
    }
    void HienThiKetQuaSua(string masach)
    {
       
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_masach",masach)
        };

        // get sach by MaSach
        dtsanpham = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getSachByMaSach", thamso);

        txtmasach.Text = dtsanpham.Rows[0]["MaSach"].ToString();
        txttensach.Text = dtsanpham.Rows[0]["TenSach"].ToString();

        //them vao
        FillLoaiSach();
        FillNXB();
       

        DropDownListmaNXB.SelectedValue = dtsanpham.Rows[0]["MaNhaXB"].ToString();
        DropDownListmaloai.SelectedValue = dtsanpham.Rows[0]["MaLoaiSach"].ToString();
        txttacgia.Text = dtsanpham.Rows[0]["TacGia"].ToString();
        txtdongia.Text = dtsanpham.Rows[0][5].ToString();
        txtsoluong.Text = dtsanpham.Rows[0]["SoLuong"].ToString();
        Image1.ImageUrl = "~/image/"+dtsanpham.Rows[0]["Hinh"].ToString();
        //FileUpload1.PostedFile.ContentType = dtsanpham.Rows[0]["Hinh"].ToString();
        txtngaydang.Text = dtsanpham.Rows[0]["NgayDang"].ToString();
        //txtngaydang.Text = String.Format("{d}", dtsanpham.Rows[0]["NgayDang"].ToString()); ;
        txtnoidung.Text = dtsanpham.Rows[0]["NoiDung"].ToString();
        txtnamxb0.Text = dtsanpham.Rows[0]["namxb"].ToString();
        txtngaydang.Enabled = false;

        MultiView1.ActiveViewIndex = 1;
    }

    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.HienThiDanhSach();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnupdate.Enabled = true;
        btnupdate.Visible = true;
        btninsert.Visible = false;
        string masach = GridView1.SelectedRow.Cells[2].Text;
        

        this.HienThiKetQuaSua(masach);
    }

    protected void btninsert_Click(object sender, EventArgs e)
    {

        string masach = txtmasach.Text.Trim();
        string tensach = txttensach.Text;
        int manxb = int.Parse(DropDownListmaNXB.SelectedValue);
        int maloaisach = int.Parse(DropDownListmaloai.SelectedValue);
        string tacgia = txttacgia.Text;
        int dongia = int.Parse(txtdongia.Text.Trim());
        int soluong = int.Parse(txtsoluong.Text.Trim());
        string hinh = FileUpload1.FileName;
        DateTime ngaydang = System.DateTime.Now;
        string noidung = txtnoidung.Text;
        int namxb = int.Parse(txtnamxb0.Text.Trim());

       
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("P_MASACH",masach),
            new OracleParameter("P_TENSACH",tensach),
            new OracleParameter("P_MANHAXB",manxb),
            new OracleParameter("P_MALOAISACH",maloaisach),
            new OracleParameter("P_TACGIA",tacgia),
            new OracleParameter("P_DONGIA",dongia),
            new OracleParameter("P_SOLUONG",soluong),
            new OracleParameter("P_HINH",hinh),
            new OracleParameter("P_NGAYDANG",ngaydang),
            new OracleParameter("P_NOIDUNG",noidung),
            new OracleParameter("P_NAMXB",namxb)

            
        };

        // insert Sach
        DataConnection.GetDatatableByStoreProcedureName("insertSach", thamso);
        
        this.HienThiDanhSach();


    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string masach = GridView1.Rows[e.RowIndex].Cells[2].Text;

        try
        {
            
            OracleParameter[] thamso = new OracleParameter[]
            {
                new OracleParameter("P_MASACH",masach),
            };

            DataConnection.GetDatatableByStoreProcedureName("Delete_SachByMaSach", thamso);
        }
        catch
        {
            System.Console.WriteLine("khong xoa duoc sach");
        }
        HienThiDanhSach();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
}